package com.hfzy.ihk.web.freeswitchManage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.facade.freeswitchManage.entity.Userinfo;
import com.hfzy.ihk.facade.freeswitchManage.request.QueryUserinfosRequest;
import com.hfzy.ihk.facade.freeswitchManage.response.QueryUserinfosResponse;
import com.hfzy.ihk.web.freeswitchManage.mapper.UserinfoMapper;
import com.hfzy.ihk.facade.freeswitchManage.service.IUserinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wws
 * @since 2018-12-29
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements IUserinfoService {

    @Autowired
    UserinfoMapper userinfoMapper;

    @Override
    public QueryUserinfosResponse queryUserinfos(QueryUserinfosRequest request) {
        QueryUserinfosResponse response = new QueryUserinfosResponse();

        try {
            QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
            if (StringTools.isNotempty(request.getEnrollNumber())) {
                queryWrapper.like("enroll_number", request.getEnrollNumber().trim());
            }

            if (StringTools.isNotempty(request.getName())) {
                queryWrapper.like("name", request.getName().trim());
            }

            if (StringTools.isNotempty(request.getUsername())) {
                queryWrapper.like("username", request.getUsername().trim());
            }

            Page page = new Page();

            if (request.getPageSize() != null && request.getPageSize() > 0) {
                page.setSize(request.getPageSize());
            }

            if (request.getPage() != null && request.getPage() > 0) {
                page.setCurrent(request.getPage());
            }

            page.setDesc("id");

            IPage<Userinfo> pages = userinfoMapper.selectPage(page, queryWrapper);
            response.setCode("1000");
            response.setMsg("成功");
            response.setDatas(pages.getRecords());
            response.setTotal(pages.getTotal());

        }catch (Exception e){
            response.setCode("2000");
            response.setMsg("失败");
        }finally {
            return response;
        }


    }

    @Override
    public Userinfo queryUserinfoById(int id) {
        return userinfoMapper.selectById(id);
    }
}
